<?php defined('SYSPATH') or die('No direct script access.');
 
class valid extends valid_Core {
 
    public static function isNull($value) {
        return is_null($value);
    }
    
    public static function isEmpty($value) {
        return is_null($value) || $value == '' || (is_array($value) && count($value) == 0);
    }
    
    public static function isWhite($value) {
        return is_null($value) || strlen(trim($value)) == 0;
    }
    
    /**
     * @param regexp je Perl kompatibilny regularny vyraz.
     */
    public static function matches($value, $regexp) {
        return preg_match($regexp, $value);
    }
    
    public static function isInteger($value) {
        $re = '/^0|([-]?[1-9][0-9]*)$/';
        return preg_match($re, $value);
    }

    public static function isPositiveInteger($value) {
        return is_numeric($value) && intVal($value) > 0;
    }

    public static function isPositiveIntegerOrZero($value) {
        return is_numeric($value) && intVal($value) >= 0;
    }

    public static function isDecimal($value) {
        return is_numeric($value);
    }

    public static function isDecimalZero($value) {
        return is_numeric($value);
    }

    public static function isPositiveDecimalOrZero($value) {
        return is_numeric($value) && floatval($value) >= 0;
    }

    public static function isIndex($value) {
        return is_numeric($value) && $value >= 0;
    }
    
    public static function isId($value) {
        $re = '/^([1-9][0-9]*)$/';
        return preg_match($re, $value);
    }
    
    public static function isIdOrEmpty($value) {
        if (self::isEmpty($value)) {
            return true;
        }
        $re = '/^([1-9][0-9]*)$/';
        return preg_match($re, $value);
    }
    
    public static function isWord($value) {
        $re = '/^[a-zA-Z0-9_-]+$/';
        return preg_match($re, $value);
    }

    public static function isCode($value) {
        return self::isWord($value);
    }
    
    public static function isEmail($value) {
        $re = '/^.+@.+\\..+$/';
        return preg_match($re, $value);
    }
    
    public static function isUrl($value) {
        $re = '/^[a-z]+:\\/\\/.+$/';
        return preg_match($re, $value);
    }
    
    public static function isDate($value) {
        if (is_array($value)) {
        	var_dump($value);
            return false;
        }
        
        $matches = preg_match(Kohana::lang('dateTimeFormat.pattern'), $value);
        if (! $matches) {
            return FALSE;
        }
        return TRUE;
    }
    
    public static function isIsoDate($value) {
        if (is_array($value)) {
            return false;
        }
        $re = '/^[12][0-9]{3}-[01][0-9]-[0123][0-9]$/';
        return preg_match($re, $value);
    }
    
    public static function isDateOrNull($value) {
        return $value == NULL || valid::isDate($value);
    }
    
    public static function isIsoDateOrNull($value) {
        return $value == NULL || valid::isIsoDate($value);
    }
    
    public static function isYear($value) {
        if (! is_numeric($value)) {
            return false;
        }
        $re = '/^[12][0-9]{3}$/';
        return preg_match($re, $value);
    }

    public static function isTime($value) {
        if (is_array($value)) {
            return false;
        }
        // Parse the time and recreate it using correct pattern. If the input value
        // and the result are the same, the $value contains a time value.
        $unixTime = strtotime($value);
        if ($unixTime === FALSE) {
            return FALSE;
        }
        if (date(Kohana::lang('dateTimeFormat.timeFormat'), $unixTime) == $value) {
            return TRUE;
        }
        return FALSE;
    }
    
    public static function isTimeOrNull($value) {
        return $value == NULL || valid::isTime($value);
    }

    public static function isSqlBoolean($value) {
        return $value !== null && trim($value) !== '' && $value == sql::$FALSE || $value == sql::$TRUE;
    }
}
 
?>
